//
//  PigTrusteePriceView.m
//  PigMarket
//
//  Created by mac on 2019/4/9.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PigTrusteePriceView.h"
#import "TrusteePriceCell.h"

@interface PigTrusteePriceView ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**
 collectionView
 */
@property (nonatomic , strong) UICollectionView *collectionView;

@end
@implementation PigTrusteePriceView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initAction];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
     [self addSubview:self.collectionView];
}
- (void)initAction {
   
}
- (void)initLayout {
    // 布局子控件
     [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

}
#pragma mark - delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    TrusteePriceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TrusteePriceCell" forIndexPath:indexPath];
    if (self.dataArr.count > 0 && self.dataArr.count > indexPath.item) {
        cell.model = self.dataArr[indexPath.item];
    }
    
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AdoptDetail *model = self.dataArr[indexPath.row];

    if (!model.isSelect) {//当前点击cell为未点击状态

        for (int i =0;i < indexPath.row + 1 ; i++) {//选中item之前都为选中状态
            AdoptDetail *model = self.dataArr[i];
            model.isSelect = YES;
         }
    } else {//已点击状态，点击取已点击状态
        NSUInteger max = self.dataArr.count - 1;
        for (NSUInteger i = max; i > 0; i--) {
            AdoptDetail *model = self.dataArr[i];

            if (i > indexPath.row) {
                model.isSelect = NO;
            }
        }
    }
    
    //清空总金额
    float trusteeCount = 0;
    for (int i = 0; i < self.dataArr.count; i++) {
        AdoptDetail *moneyModel = self.dataArr[i];
        if (moneyModel.isSelect) {
            if ([moneyModel.type isEqualToString:@"1"]) {
                float  price = [moneyModel.money floatValue];
                trusteeCount += price;
            } else if ([moneyModel.type isEqualToString:@"2"]) {//总包
                trusteeCount = [moneyModel.money floatValue];
            }
        }
    }
    
    [self.collectionView reloadData];

    NSString *priceStr = [NSString stringWithFormat:@"%.2f",trusteeCount];
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationTrusteePriceChange object:self userInfo:@{@"selectTrusteePrice":priceStr}];
}

#pragma mark - getter and setter
- (void)setDataArr:(NSMutableArray *)dataArr {
    _dataArr = dataArr;
    [self.collectionView reloadData];
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemW = (SCREEN_WIDTH - 100) / 3;
        CGFloat itemH = 120;
        layout.itemSize = CGSizeMake(itemW, itemH);
        layout.sectionInset = UIEdgeInsetsMake(15, 20, 0, 20);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[TrusteePriceCell class] forCellWithReuseIdentifier:@"TrusteePriceCell"];
    }
    return _collectionView;
}

@end
